var getCookie = readCookie("user")
if(!getCookie){
	location.href="login.htm"
}
var getCid=location.search
var getCount=0
var obj=new Object()
var getNum=getCid.substring(getCid.indexOf("cNum=")+5,getCid.indexOf("&id"))//详情页面跳转过来
	initVue()
	
	getAddress()
	
	if(getCid.indexOf("cNum=")>=0&&getCid.indexOf("&id")>=0){
		initData()	
	}else{
		var sId=getCid.substring(getCid.indexOf("?cIds")+6)
		sinitData(sId)
	}

$(function(){
	buyCommodity()
	$(':input').labelauty();
	
})
 function initVue(){
	vm=new Vue({
		el:"#Orders",
		data:{
			sinfo:[],
			adinfo:[],
			cNum:[{"num":getNum}]
		},
		methods:{
			backTarget(){
				location.href="shopping_cart.html"
			},
			buyCommodity(){
				buyCommodity()
		}
		}
	})
	
} 
function buyCommodity(){
	
	var address=$("input[name=address]:checked").val()//收货地址
	
	var queck=$("input[name=express]:checked").val()//快递方式
	var play=$("input[name=payType]:checked").val()//支付方式
	var uId=getCookie//用户id
	obj.getNum1=""
	obj.getPrice=""
	obj.getCid1=""
	for(var i=0;i<getCount;i++){
		
		var getNum1=$("#num"+i).val()
		var getCid1=$("#id"+i).val()
		var getPrice=$("#price"+i).val()
		//alert(getCount>1&&i>=1)
		if(getCount==1){
			obj.getCid1=getCid1
			obj.getPrice=getPrice
			obj.getNum1=getNum1
			
		}else if(getCount>1){
			
			obj.getNum1=getNum1+","+obj.getNum1
			obj.getPrice=getPrice+","+obj.getPrice
			obj.getCid1=getCid1+","+obj.getCid1
		}
		
		//submitData()
		//cutCommodity()
		//submitData()
		}
	
		obj.getAddress=address
		obj.getQueck=queck
		obj.getPlay=play
		obj.getUId=uId
		//alert()
		$("#uId").val(obj.getUId)
		$("#adress").val(obj.getAddress)
		$("#cId").val(obj.getCid1)
		$("#cNum").val(obj.getNum1)
		$("#oPrice").val(obj.getPrice)
		$("#address").val(address)
		

}
function submitData(){
	
	 $.ajax({
		url:"http://localhost:8080/OrderForm/add",
		data:{"uId":obj.getUId,
		"adress":obj.getAddress,
		"cId":obj.getCid1,
		"cNum":obj.getNum1,
		"express":obj.getQueck,
		"payType":obj.getPlay,
		"oPrice":obj.getPrice
		},
		type:"post",
		dataType:"json",
		async:false,
		success:function(data){
			window.open('about:blank');
			if(data.status==1){
				$.DialogByZ.Autofade({Content: "下单完成"})
			}else{
				$.DialogByZ.Alert({Title: "提示", Content: data.msg,BtnL:"确定"})
			}
		
		}
	}) 
}

function sinitData(sId){//购物车页面跳转购买
	$.ajax({
		url:"http://localhost:8080/shopcar/selectByIn",
		type:"post",
		async:false,
		dataType:"json",
		data:{
			"cIds":sId,
			"uId":getCookie
		},
		success:function(data){
			$("#myTable").html("")
			var myPrice=0.0
			getCount=data.data.length
			for(var i=0;i<data.data.length;i++){
					myPrice+=(data.data[i].cPrice*data.data[i].cNum)
				 $("#myTable").append("<thead><tr class='title'><input type='hidden' id='price"+i+"' value='"+data.data[i].sPrice+"'/><input type='hidden' id='id"+i+"' value='"+data.data[i].cId+"'/> <input type='hidden' id='num"+i+"' value='"+data.data[i].cNum+"'/><td class='name'>商品名称</td><td class='price'>商品价格</td><td class='Quantity'>购买数量</td><td class='Money'>金额</td></tr></thead><tbody><tr v-for='info in sinfo' > <td class='Product_info'><a href='#'><img src=http://localhost:8080/"+data.data[i].cImg+"  width='100px' height='100px'/></a><a href='#' class='product_name'  style='text-align: center;'>"+data.data[i].cName+"</a> </td><td><i>￥</i>"+data.data[i].cPrice+"</td><td v-for='cnumber in cNum'>"+data.data[i].cNum+"</td><td v-for='cnumber in cNum'><i>￥</i>"+(data.data[i].cPrice*data.data[i].cNum)+"</td></tr></tbody>")
				 
			}
			
			 $("#sPrice").html(myPrice)
			$("#myPrice").html(myPrice) 
		}
	})
}
function initData(){
	$.ajax({
		url:"http://localhost:8080/commodity/chooseCommodity"+getCid,
		type:"get",
		async:false,
		dataType:"json",
		success:function(data){
			var myPrice=0.0
			vm.$data.sinfo=data.data
			getCount=data.data.length
			for(var i=0;i<data.data.length;i++){
				myPrice+=(data.data[i].activity_prices*parseFloat(getNum))
			
			}
			$("#sPrice").html(myPrice)
			$("#myPrice").html(myPrice) 
	
		}
	})
} 

function getAddress(){
	$.ajax({
		url:"http://localhost:8080/address/selectByUid",
		data:{"uId":getCookie},
		type:"get",
		dataType:"json",
		async:false,
		success:function(data){
			
			
			vm.$data.adinfo=data.data
			
		
		}
	})
}